package com.shuda.business.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shuda.business.monitor.entity.*;
import com.shuda.business.monitor.service.MonitorService;
import com.shuda.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 数据监控控制器
 */
@RestController
@RequestMapping("/api/monitor")
@RequiredArgsConstructor
@Tag(name = "数据监控", description = "数据监控相关接口")
public class MonitorController {

    private final MonitorService monitorService;

    /**
     * 获取统计数据
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取统计数据")
    @PreAuthorize("hasAuthority('monitor:view')")
    public Result<StatisticsEntity> getStatistics() {
        return Result.success(monitorService.getStatistics());
    }

    /**
     * 获取趋势数据
     */
    @GetMapping("/trend")
    @Operation(summary = "获取趋势数据")
    @PreAuthorize("hasAuthority('monitor:view')")
    public Result<List<TrendDataEntity>> getTrendData(
            @Parameter(description = "开始时间") @RequestParam LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam LocalDateTime endTime,
            @Parameter(description = "统计类型") @RequestParam String type) {
        return Result.success(monitorService.getTrendData(startTime, endTime, type));
    }

    /**
     * 获取渠道统计
     */
    @GetMapping("/channel/statistics")
    @Operation(summary = "获取渠道统计")
    @PreAuthorize("hasAuthority('monitor:view')")
    public Result<List<ChannelStatisticsEntity>> getChannelStatistics(
            @Parameter(description = "开始时间") @RequestParam LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam LocalDateTime endTime) {
        return Result.success(monitorService.getChannelStatistics(startTime, endTime));
    }

    /**
     * 获取实时监控数据
     */
    @GetMapping("/realtime")
    @Operation(summary = "获取实时监控数据")
    @PreAuthorize("hasAuthority('monitor:view')")
    public Result<IPage<RealtimeDataEntity>> getRealtimeData(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "渠道ID") @RequestParam(required = false) Long channelId,
            @Parameter(description = "状态") @RequestParam(required = false) String status,
            @Parameter(description = "开始时间") @RequestParam(required = false) LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam(required = false) LocalDateTime endTime) {
        return Result.success(monitorService.getRealtimeData(pageNum, pageSize, channelId, status, startTime, endTime));
    }

    /**
     * 获取错误统计
     */
    @GetMapping("/error/statistics")
    @Operation(summary = "获取错误统计")
    @PreAuthorize("hasAuthority('monitor:view')")
    public Result<List<ErrorStatisticsEntity>> getErrorStatistics(
            @Parameter(description = "开始时间") @RequestParam LocalDateTime startTime,
            @Parameter(description = "结束时间") @RequestParam LocalDateTime endTime) {
        return Result.success(monitorService.getErrorStatistics(startTime, endTime));
    }

    /**
     * 获取性能指标
     */
    @GetMapping("/performance")
    @Operation(summary = "获取性能指标")
    @PreAuthorize("hasAuthority('monitor:view')")
    public Result<PerformanceMetricsEntity> getPerformanceMetrics() {
        return Result.success(monitorService.getPerformanceMetrics());
    }

    /**
     * 获取告警配置
     */
    @GetMapping("/alert/config")
    @Operation(summary = "获取告警配置")
    @PreAuthorize("hasAuthority('monitor:alert:view')")
    public Result<List<AlertConfigEntity>> getAlertConfig() {
        return Result.success(monitorService.getAlertConfig());
    }

    /**
     * 更新告警配置
     */
    @PutMapping("/alert/config/{id}")
    @Operation(summary = "更新告警配置")
    @PreAuthorize("hasAuthority('monitor:alert:update')")
    public Result<Boolean> updateAlertConfig(
            @Parameter(description = "配置ID") @PathVariable Long id,
            @RequestBody AlertConfigEntity config) {
        return Result.success(monitorService.updateAlertConfig(id, config));
    }

    /**
     * 获取告警记录
     */
    @GetMapping("/alert/records")
    @Operation(summary = "获取告警记录")
    @PreAuthorize("hasAuthority('monitor:alert:view')")
    public Result<IPage<AlertRecordEntity>> getAlertRecords(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页条数") @RequestParam(defaultValue = "20") Integer pageSize,
            @Parameter(description = "告警级别") @RequestParam(required = false) String alertLevel,
            @Parameter(description = "处理状态") @RequestParam(required = false) String status) {
        return Result.success(monitorService.getAlertRecords(pageNum, pageSize, alertLevel, status));
    }

    /**
     * 处理告警
     */
    @PutMapping("/alert/records/{id}/handle")
    @Operation(summary = "处理告警")
    @PreAuthorize("hasAuthority('monitor:alert:handle')")
    public Result<Boolean> handleAlert(
            @Parameter(description = "告警记录ID") @PathVariable Long id,
            @Parameter(description = "处理意见") @RequestParam String handleRemark) {
        return Result.success(monitorService.handleAlert(id, handleRemark));
    }
}